package com.zjhb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zjhb.common.dao.BaseDao;
import com.zjhb.common.dao.Page;
import com.zjhb.common.enumutil.Area;
import com.zjhb.common.enumutil.Asets;
import com.zjhb.common.enumutil.UserTypes;
import com.zjhb.common.enumutil.Verify;
import com.zjhb.common.util.JwtUser;
import com.zjhb.common.util.ResourceUtil;
import com.zjhb.common.util.ResponseUtil;
import com.zjhb.common.util.StringMyUtil;
import com.zjhb.common.util.TokenUtil;
import com.zjhb.entity.AssetsEntity;
import com.zjhb.entity.FileEntity;
import com.zjhb.entity.KeepApplyEntity;
import com.zjhb.entity.KeepInfoEntity;
import com.zjhb.entity.UserEntity;

import io.jsonwebtoken.lang.Collections;
import param.AssetParam;
import param.Codes;

@RestController
@RequestMapping("/app")
public class AppController {

	@Autowired
	private BaseDao baseDao;

	@PostMapping("/login")
	public JSONObject appLogin(HttpServletRequest req, HttpServletResponse response) {
		// 语言选择
		JSONObject j = new JSONObject();
		// 验证码
		Criteria crt = baseDao.getCriteria(UserEntity.class);
		String name = req.getParameter("userName");
		String password = req.getParameter("password");
		crt.add(Restrictions.eq("name", name)).add(Restrictions.eq("password", password));
		List<UserEntity> list = crt.list();
		if (list.size() == 0) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "用户或密码不正确", "");
			return j;
		} else {
			UserEntity user = list.get(0);
			if (user.getStatus() == "0") {
				j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "该账号已冻结", "");
				return j;
			}
			JwtUser jwtuser = new JwtUser();
			jwtuser.setId(user.getId());
			jwtuser.setName(user.getName());
			jwtuser.setUserType(user.getTypes());
			jwtuser.setDepartId(user.getDepatmentId());
			String token = "";
			Map<String, Object> map = new HashMap<>();
			if (user.getTypes().equals(UserTypes.Admin.getKey())) {
				token = TokenUtil.createJWT(jwtuser);
				map.put("token", token);
				map.put("type", user.getTypes());
				map.put("name", user.getName());
			} else if (user.getTypes().equals(UserTypes.Keep.getKey())) {
				token = TokenUtil.createJWT(jwtuser);
				map.put("token", token);
				map.put("type", user.getTypes());
				map.put("name", user.getName());
			} else {
				j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "该账号不允许登陆", "");
				return j;
			}
			j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "登陆成功", map);
			return j;

		}
	}

	// 获取当前人员维修记录
	@PostMapping("/keep/list")
	public JSONObject keepList(HttpServletRequest req, HttpServletResponse response) {
		JSONObject j = new JSONObject();
		Criteria crtKeep = baseDao.getCriteria(KeepApplyEntity.class);
		crtKeep.add(Restrictions.eq("status", Verify.WaitKeep.getKey()))
				.add(Restrictions.eq("keepUserId", ResourceUtil.getCurrentUser().getId()));
		List<KeepApplyEntity> keepList = crtKeep.list();
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "登陆成功", keepList);
		return j;

	}

	// 统一提交维修
	@PostMapping("/keep/submit")
	public JSONObject keepSubmit(KeepInfoEntity keep) {
		JSONObject j = new JSONObject();
		KeepApplyEntity keepApplyEntity = baseDao.getById(KeepApplyEntity.class, keep.getKeepApplyId());
		if (keepApplyEntity == null) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "当前维修申请不存在", "");
			return j;
		}
		if (keepApplyEntity.getStatus().equals(Verify.Finish.getKey())) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "当前维修单已完成", "");
			return j;
		}
		baseDao.save(keep);
		if (!Collections.isEmpty(keep.getWjIds())) {
			for (String wjId : keep.getWjIds()) {
				FileEntity file = baseDao.getById(FileEntity.class, wjId);
				file.setTableName(keep.getId());
			}

		}
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", "");
		return j;
	}

	// 统一提交code
	@PostMapping("/count")
	public JSONObject code(Codes codes) {
		JSONObject j = new JSONObject();
		Criteria crtKeep = baseDao.getCriteria(AssetsEntity.class);
		crtKeep.add(Restrictions.eq("departId", codes.getDepartId())).add(Restrictions.eq("areaId", codes.getAreaId()));
		List<AssetsEntity> list = crtKeep.list();
		List<String> listCodes = codes.getCode();
		List<String> resErro = new ArrayList<>();
		for (AssetsEntity ass : list) {
			if (!listCodes.contains(ass.getCode())) {
				resErro.add(ass.getName() + ass.getCode() + "未盘点");
			}
		}
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", resErro);
		return j;
	}
	
	@PostMapping("/list")
	public JSONObject list(HttpServletRequest req, HttpServletResponse resp, AssetParam param) throws Exception {
		JSONObject j = new JSONObject();
		Page<AssetsEntity> page = new Page<>();
		Criteria crt = baseDao.getCriteria(AssetsEntity.class);	
	    List<AssetsEntity> list = crt.list();
		Long totalNum = baseDao.getCount(crt);
		page.setRows(list);
		page.setTotal(totalNum);
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", page);
		return j;
	}

}
